package net.sf.midollo.net.io;

import java.io.IOException;

import javax.microedition.io.CommConnection;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.io.StreamConnection;

import net.sf.midollo.io.IOStream;
import net.sf.midollo.io.IOStreamFactory;
import net.sf.midollo.utils.MidpUtils;
import net.sf.midollo.utils.Utils;

public class StreamConnectionIOStreamFactory implements IOStreamFactory {

  protected String name;

  public StreamConnectionIOStreamFactory(String name) {
    this.name = name;
  }

  public IOStream open() throws IOException {
    Utils.log("open " + this.name);
    StreamConnection sc = (StreamConnection)Connector.open(this.name);
    if (sc instanceof SocketConnection) {
      SocketConnection soc = (SocketConnection)sc;
      Utils.log(MidpUtils.getSocketConnectionId(soc));
      Utils.log(MidpUtils.getSocketOptions(soc));
    } else if (sc instanceof CommConnection) {
      CommConnection cc = (CommConnection)sc;
      Utils.log(MidpUtils.getCommOptions(cc));
    }
    return new StreamConnectionIOStream(sc);
  }
}
